// 新增同名项目设置功能 20211019 耿琰
import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 			= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
const EnableData = [{name:'启用',value:'1'},{name:'禁用',value:'0'}]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '岗位',		name: 'project_id',		type: 'select', loading:true },
		],
		// 提交表单列表
		formData : [
			{ label: '岗位名称',		name: 'project_id', 	full:true, type:'select', required:true  },
			{ label: '项目名称',		name: 'kind_id', 		full:true, type:'select', mode:'multiple', required:true, count:null },
			{ label: '状态',		name: 'enabled', 		full:true, type:'select', data:EnableData },
			{ label: '描述',		name: 'memo', 			full:true, type:'textarea' },
		],
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	//获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		
		$cache.then( async f => {
			// 岗位
			$fn.setRows( [forms, formData], 'project_id', await f.getCache({ api:'gang_wei' } ) )
			// 项目名称
			$fn.setRow(formData, 'kind_id', await f.getCache({ api:'project_name' } ))
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'lis-project-kind/index')
	
	// 表单
	cols = [
		{ title: '岗位名称',		dataIndex: 'project_name', 		width:100 },
		{ title: '岗位编号', 	dataIndex: 'project_code', 		width:150 },
		{ title: '项目名称', 	dataIndex: 'kind_names', 		width:150 },
		// { title: '项目编号', 	dataIndex: 'en_name', 			width:150 },
		{ title: '备注', 		dataIndex: 'memo', 				width:150, align:'center' },
		{ title: '状态', 		dataIndex: 'enabled_name', 		...$fn.table.status },
		TableControls.controls.call(this,{ width: 150 })
	]
	
	// 表格操作按钮
	table_controls = (text,{ uuid, enabled } ) => {
		return [
			TableControls.edit.call(this, 'lis-project-kind/info', { uuid }),			
			+enabled === 0 && this.onEnabled( 1,  uuid ),	// 启用
			+enabled === 1 && this.onEnabled( 0,  uuid ),		// 禁用
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this),			// 添加
		]
	}
	
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, on:'lis-project-kind/on', off:'lis-project-kind/off', isKeys: !uuid, onComplete:()=>$fn.dataClear() })
		
	render(){
		return (
			<>
				<Page title='同名项目设置' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{type:-1,isInit:window.isInit}) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'lis-project-kind/add', editApi:'lis-project-kind/edit' }) }
			</>
		)
	}
}

export default Index